package jdk_interface;

/**
 * @author yuisama
 * @date 2022/09/18 21:25
 **/

class Money implements Cloneable{
    double sal = 100.5;

    public Money clone() {
        try {
            return (Money) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}
public class Person implements Cloneable{
    private String name;
    private int age;
    Money money = new Money();
    public static void main(String[] args) {
        Person per = new Person("铭哥",18);
        // 这个不是对象clone，两个引用，一个对象
        Person per1 = per.clone();
        System.out.println(per1);
        System.out.println(per);
        per1.showPer();
        per.showPer();
        System.out.println("修改per的money对象值");
        per.money.sal = 66.6;
        System.out.println(per.money.sal);
        System.out.println(per1.money.sal);
    }
    public Person clone() {
        Person per = null;
        try {
            // Object类的clone方法，所有对象都有，但是只有实现了Cloneable接口的子类才能真正使用
            per = (Person) super.clone();
            per.money = money.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        return per;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void showPer() {
        System.out.println("this.name = " + name + ",this.age = " + age + ",sal = " + money.sal);
    }


}